package com.baimawangzi.recovery.service.impl;

import com.baimawangzi.recovery.bean.Goods;
import com.baimawangzi.recovery.bean.PageBean;
import com.baimawangzi.recovery.dao.GoodsDao;
import com.baimawangzi.recovery.dao.impl.GoodsDaoImpl;
import com.baimawangzi.recovery.service.GoodsService;

import java.util.List;

public class GoodsServiveImpl implements GoodsService {
    private GoodsDao goodsDao =new GoodsDaoImpl();
    @Override
    public List<Goods> findByLimit(int m, int n) {
        return goodsDao.findByLimit(m,n);
    }

    @Override
    public int add(Goods goods) {
        return goodsDao.add(goods);
    }

    @Override
    public int delete(int id) {
        return goodsDao.delete(id);
    }

    @Override
    public Goods findById(int id) {
        return goodsDao.findById(id);
    }

    @Override
    public int update(Goods goods) {
        return goodsDao.update(goods);
    }

    @Override
    public PageBean<Goods> getPageBean(int currentPage, int pageSize) {
        PageBean<Goods> pageBean = new PageBean<>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalSize(goodsDao.getCount());
        if (pageBean.getTotalSize()%pageSize==0){
            pageBean.setTotalPage(pageBean.getTotalSize()/pageSize);
        }
        else {
            pageBean.setTotalPage(pageBean.getTotalSize()/pageSize+1);
        }
        int m =(currentPage-1)*pageSize;
        pageBean.setData(goodsDao.findByLimit(m,pageSize));
        return pageBean;
    }
}
